package main

import (
	"fmt"
	"hash/crc32"
	"strconv"
	"strings"
	"sync"
)

type Feq map[rune]int

var lock sync.RWMutex

func main() {
	str := `{"biz_type":"api_gateway_notify","msg_type":"markdown","title":"\"prometheus\\u544a\\u8b66\"","content":"\"\\n- \\u544a\\u8b66\\u5b9e\\u4f8b: test-gateway-ua-api.testxinfei.cn:80\\n- \\u544a\\u8b66\\u72b6\\u6001\\uff1afiring\\n- \\u544a\\u8b66\\u540d\\u79f0: rabbitmq\\u961f\\u5217\\u6d88\\u606f\\u5806\\u79ef\\n- \\u544a\\u8b66\\u8be6\\u60c5: \\u6d4b\\u8bd5-rabbitmq\\u961f\\u5217\\u6d88\\u606f\\u5806\\u79ef\\uff0c\\u961f\\u5217\\u540d\\uff1aapi.gateway.51gjjapproval.queue\\napi.gateway.hbeiOcrHandle.queue\\napi.gateway.jqns.queue\\n- \\u6545\\u969c\\u65f6\\u95f4: 14:15:40\\n- \\u6062\\u590d\\u65f6\\u95f4: \\n- \\u67e5\\u770b\\u8be6\\u60c5: [\\u70b9\\u51fb\\u67e5\\u770b](http://192.168.21.73:9090/graph?g0.expr=xyf_api_gateway_rabbitmq_lag{instance=\"test-gateway-ua-api.testxinfei.cn:80\"} \u003e 50\u0026g0.tab=0)\\n![](https://xyf-test.oss-cn-beijing.aliyuncs.com/prometheus/amkngzbJyEfrgYQM.png?Expires=1643192439\u0026OSSAccessKeyId=LTAI4GFYbsssC2gEQMXMDPZk\u0026Signature=FTIbFshRTvRxYDkSI5QrI1IGmsc=)\\n=======================\\n\\n- \\u544a\\u8b66\\u5b9e\\u4f8b: test-gateway-ua-api.testxinfei.cn:80\\n- \\u544a\\u8b66\\u72b6\\u6001\\uff1afiring\\n- \\u544a\\u8b66\\u540d\\u79f0: P90 \u003e 2s \\u63a5\\u53e3\\n- \\u544a\\u8b66\\u8be6\\u60c5: P90 \u003e 2s\\u63a5\\u53e3\\uff0c\\u63a5\\u53e3\\u5730\\u5740\\uff1aaapi/after/repay\\naapi/before/approval\\naapi/middle/calculator\\naapi/middle/confirm-loan\\naapi/middle/verify-bind-card-sms\\n- \\u6545\\u969c\\u65f6\\u95f4: 14:49:55\\n- \\u6062\\u590d\\u65f6\\u95f4: \\n- \\u67e5\\u770b\\u8be6\\u60c5: [\\u70b9\\u51fb\\u67e5\\u770b](http://192.168.21.73:9090/graph?g0.expr=histogram_quantile(0.9, sum by(le, request_path) (rate(xyf_api_gateway_http_requests_bucket[10m]))) \u003e 2\u0026g0.tab=0)\\n![](https://xyf-test.oss-cn-beijing.aliyuncs.com/prometheus/JcIHzQWSGkiFujkO.png?Expires=1643192440\u0026OSSAccessKeyId=LTAI4GFYbsssC2gEQMXMDPZk\u0026Signature=WKeKKoXcoIXaK71ibFSG6VRRkvA=)\\n=======================\\n\"","at_mobiles":["18721586775"],"at_all":false}`
	str = strings.Replace(str, "\\u0026", "&", -1)

	fmt.Println(str)

	sText := "hello 你好"
	textQuoted := strconv.QuoteToASCII(sText)
	textUnquoted := textQuoted[1 : len(textQuoted)-1]
	fmt.Println(textUnquoted)

	res1 := hashKey("123456")
	fmt.Println(res1)

	quit := make(chan struct{})
	strs := []string{"abc", "abcd", "efd"}
	res := counterWord(strs, 10, quit)

	<-quit
	fmt.Println(res)
}

func counterWord(strs []string, concurrent int, quit chan struct{}) Feq {
	ch := make(chan Feq, concurrent)

	for _, str := range strs {
		strtmp := str
		go counter(strtmp, ch)

	}

	//feq := make(Feq)
	//for v := range ch {
	//	for c, count := range v {
	//		feq[c] += count
	//	}
	//}

	feq := consumer(ch)

	quit <- struct{}{}
	return feq
}

func counter(str string, ch chan Feq) {
	m := make(Feq)
	for _, s := range str {
		m[s]++
	}
	ch <- m
}

func consumer(ch chan Feq) Feq {
	feq := make(Feq)
	for v := range ch {
		for word, num := range v {
			feq[word] += num
		}
	}

	return feq
}

func hashKey(key string) uint32 {
	if len(key) < 64 {
		var scratch [64]byte
		copy(scratch[:], key)
		return crc32.ChecksumIEEE(scratch[:len(key)])
	}

	return crc32.ChecksumIEEE([]byte(key))
}
